package cn.dxj1016.rabbitmqproducerconsumer.work.lunxun;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Work2 {
    private static Runnable runnable = new Runnable() {
        public void run() {
//        所有的中间件技术都是基于tcp/ip协议基础之上构建新型的协议规范，只不过rabbitmq遵循的是amqp协议
//        ip port
//        1：创建连接工程
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("localhost");
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("guest");
            connectionFactory.setPassword("guest");
            connectionFactory.setVirtualHost("/");
//         获取队列的名称
            final String queueName = Thread.currentThread().getName();
            Connection connection = null;
            Channel channel = null;
//        2：创建连接Connection
            try {
                connection = connectionFactory.newConnection("消费者-Work2");
//        3：通过连接获取通道Channel
                channel = connection.createChannel();
//        4：定义接收消息的回调
                final Channel finalChannel = channel;
                finalChannel.basicConsume("queue1", true, new DeliverCallback() {
                    @Override
                    public void handle(String consumerTag, Delivery message) throws IOException {
//                        System.out.println(message.getEnvelope().getDeliveryTag());
                        System.out.println("Work2-收到消息是：" + new String(message.getBody(), "UTF-8"));
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }, new CancelCallback() {
                    @Override
                    public void handle(String consumerTag) throws IOException {
                        System.out.println("接收失败了。。。");
                    }

                });
                System.out.println("Work2-开始接收信息");
                System.in.read();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("接收消息异常");
            } finally {
//        7：关闭连接
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
//        8：关闭通道
                if (connection != null && connection.isOpen()) {
                    try {
                        connection.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    public static void main(String[] args) {
//          启动三个线程去执行
        new Thread(runnable,"queue1").start();
    }
}
/*
执行结果：
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Work2-开始接收信息
Work2-收到消息是：学相伴：2
Work2-收到消息是：学相伴：4
Work2-收到消息是：学相伴：6
Work2-收到消息是：学相伴：8
Work2-收到消息是：学相伴：10
Work2-收到消息是：学相伴：12
Work2-收到消息是：学相伴：14
Work2-收到消息是：学相伴：16
Work2-收到消息是：学相伴：18
Work2-收到消息是：学相伴：20
 */